package com.dongdongshop.config;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.pojo.TbAdminUser;
import com.dongdongshop.service.AdminService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class AdminRealm extends AuthorizingRealm {

    @Reference
    private AdminService adminService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) {
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String username = token.getUsername();
        TbAdminUser admin = adminService.getAdminByUsername(username);
        if (admin == null){
            return null;
        }
        return new SimpleAuthenticationInfo(admin,admin.getPassword(), ByteSource.Util.bytes(admin.getSalt()),"");
    }
}
